// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,
    ctor(){
        this.slideMinY = -182;
        this.slideMaxW = 324;
        this.slideMinW = 114;
        this.maxLen = 7;
        this.gameOver = false;
        this.styles = [
            {color: new cc.Color(255, 0, 0), value: 1},//红
            {color: new cc.Color(0, 255, 0), value: 2},//绿
            {color: new cc.Color(0, 0, 255), value: 3},//蓝
            {color: new cc.Color(255, 255, 0), value: 4},//黄
        ];
        this.startTime = 0;
    },
    properties: {
        slidePrefab: cc.Prefab,
        insideBox: cc.Node,
        outsideBox: cc.Node,
    },  

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        //manager.enabledDebugDraw = true;

        this.inslideList = this.insideBox.children;
        this.outslideList = this.outsideBox.children;
        this.insideData = [];
        this.inslideList.forEach((item, index)=>{
            if(index != 0){
                this.insideData.push({
                    list: [],
                    node: item,
                    x: undefined,
                    y: undefined
                })
            }
        })
        this.xLen = this.insideData.length-1;



        this.node.on(cc.Node.EventType.MOUSE_DOWN, ()=>{

            this.insideBox.angle+=60;
        })
    },
    getRnd(min, max){
        return Math.floor( Math.random()*(max-min)+min )
    },
    createSlide({group, pos, parent, style, index, tag}){
        let slide = cc.instantiate(this.slidePrefab);
        slide.getComponent(cc.BoxCollider).tag = tag || 0;
        slide.style = style;
        slide.posIndex = index;
        slide.group = group;
        slide.setPosition(pos);
        slide.on('outsideCollide', this.outsideCollide, this)
        parent.addChild(slide);
        return slide;
    },
    outsideCollide({node, tag}){
        let cur = this.insideData[tag];
        let index = cur.list.length;        
        if(index == this.maxLen){
            node.destroy();
            this.gameOver = true;
            return
        }

        let slide = this.createSlide({group: 'inside', pos: cc.v2(0,(1-index/this.maxLen)*this.slideMinY), parent: cur.node, style: node.style,  index: index, tag: tag});
        cur.list.push({
            node: slide,
            value: node.style.value
        })
        node.destroy();
        this.printListData();
        //尝试消除
        this.removeSlide(tag, index);
    },
    removeSlide(x, y){
        let arr = [];
        let item = this.insideData[x].list[y];
        arr.push(item);
        item.find = true;
        this.findSlide(item, x, y, arr);
        console.log(arr.length)
        if(arr.length < 3){
            arr.map(item=>{
                item.find = false;
            })
            arr = [];
        }else{
            let arr2 = [];
            arr.map(item=>{
                item.node.destroy();
                let y = item.y+1;
                while(this.insideData[item.x].list[y] && !this.insideData[item.x].list[y].find){
                    arr2.push(this.insideData[item.x].list[y].node)
                    ++y;
                }
            })
            arr.map(item=>{
                while(this.insideData[item.x].list[item.y]){
                    this.insideData[item.x].list.splice(item.y, 1);
                } 
            }) 
            for(let i=0; i<arr2.length; i++){
                setTimeout(()=>{
                    arr2[i].emit('move')
                }, i*30)
            }
        }
    },
    findSlide(item, x, y, arr){
        item.x = x;
        item.y = y;
        let lx = x==0?this.xLen:x-1;
        let l = this.insideData[lx].list[y];
        if(l && l.value == item.value && !l.find){
            arr.push(l)
            l.find = true;
            this.findSlide(l, lx, y, arr)
        }

        let rx = x==this.xLen?0:x+1;
        let r = this.insideData[rx].list[y];
        if(r && r.value == item.value && !r.find){
            arr.push(r)
            r.find = true;
            this.findSlide(r, rx, y, arr)
        }

        let t = y==this.maxLen?null:this.insideData[x].list[y+1];
        if(t && t.value == item.value && !t.find){
            arr.push(t)
            t.find = true;
            this.findSlide(t, x, y+1, arr)
        }        
        let b = y==0?null:this.insideData[x].list[y-1];
        if(b && b.value == item.value && !b.find){
            arr.push(b)
            b.find = true;
            this.findSlide(b, x, y-1, arr)
        }    
    },
    //打印数据
    printListData(){
        let arr = [];
        this.insideData.forEach(item=>{
            let subArr = [];
            item.list.forEach(subItem=>{
                subArr.push(subItem.value)
            })
            arr.push(subArr)
        })
        //console.table(arr);
    },
    start () {

    },

    update (dt) {
        if(this.gameOver){
            return false;
        }
        this.startTime+=dt;
        if(this.startTime >= 1){
            this.startTime = 0;
            this.createSlide({group: 'outside', pos: cc.v2(0,0), parent: this.outslideList[0], style: this.styles[this.getRnd(0,4)]})
        }
    },
});
